Smoke Detector with XIAO RP2040 & MQ-135 Sensor | Audio Alert System

Image This project teaches you to build a smoke detector that triggers an audio alert when smoke is detected. Using the Seeed Studio XIAO RP2040 and the MQ-135 smoke sensor, we’ll create a compact system capable of detecting smoke and playing an alert through the DFPlayer Mini and speaker. This project is perfect for those looking to implement air quality monitoring and alarm systems for small spaces, learning about microcontrollers, audio modules, and sensors along the way.

Components Used Seeed Studio XIAO RP2040: A small, powerful microcontroller that’s great for compact projects. Image MQ-135 Smoke Sensor: An air quality sensor that detects various gases, making it ideal for applications focused on smoke and gas detection. Image DFPlayer Mini: A compact audio module that plays MP3 files from an SD card, allowing for an audio alert when smoke is detected. Image Speaker: Used to output the audio alert triggered by the DFPlayer Mini. I used a 20 Ohm 1 Watt speaker Image

Component Descriptions

MQ-135 Smoke Sensor The MQ-135 sensor is designed to detect gases such as ammonia (NH3), smoke, carbon dioxide (CO2), and more, making it useful for monitoring air quality in homes, factories, and offices. With its high sensitivity to various harmful gases, this sensor is a popular choice for smoke detection. Pinout Image DFPlayer Mini The DFPlayer Mini is a small audio player module that supports MP3, WAV, and WMA files. It features an SD card slot (up to 32GB in FAT16 or FAT32 format) and a direct output for connecting to a speaker, making it easy to add audio functionality to your projects. Communication with a microcontroller is done via UART. Pinout Image Wiring Connections

XIAO RP2040 to DFPlayer Mini:

Speaker to DFPlayer Mini:

XIAO RP2040 to MQ-135:

Circuit Diagram Image Code Breakdown Explanation of Each Code Section

Libraries and Serial Setup
#include "SoftwareSerial.h"
Enables the use of software serial communication, allowing us to use alternative pins for the DFPlayer Mini module.
#include"DFRobotDFPlayerMini.h"
Includes library for DFPlayer Mini, which simplifies control of the audio playback features (play, stop, set volume, etc.). Creating Serial Object and DFPlayer Instance
SoftwareSerial mySoftwareSerial(27, 26)
Initializes the software serial connection on pins 27 (RX) and 26 (TX), designated for DFPlayer communication.
DFRobotDFPlayerMini myDFPlayer
Creates a DFPlayer Mini object, enabling control over playback functions. Constants and Variable
const int analogButtonPin = A2
Defines the analog input pin connected to the MQ-135 sensor.
bool isPlaying = false
Tracks if the audio track is currently playing.
unsigned long lastPlayTime = 0
Stores the last time an audio track started playing.
const unsigned long playDuration = 3500
Defines the playback duration (in milliseconds) for the audio alert. Setup Function
mySoftwareSerial.begin(9600)
Initializes software serial at 9600 baud, setting up communication with the DFPlayer Mini.
Serial.begin(115200)
Begins communication on the Serial Monitor at 115200 baud, allowing you to monitor sensor readings and logs.
pinMode(analogButtonPin,INPUT)
Configures the MQ-135 sensor pin as an input.
if (!myDFPlayer.begin(mySoftwareSerial))
Checks if DFPlayer Mini initializes correctly. If it fails, error messages are displayed, and the program halts.
myDFPlayer.volume(30)
Sets the volume for DFPlayer Mini to the maximum level of 30.
myDFPlayer.EQ(0)
Configures the equalizer to Normal. Loop Function
int reading = analogRead(analogButtonPin)
Reads the analog signal from the MQ-135 sensor, converting it to a digital value.
Serial.println(reading)
Displays the sensor reading for debugging.
if (reading > 80)
Checks if the sensor reading is above a certain threshold (80), which indicates the presence of smoke. If isPlaying is false, it starts playing the audio track using playSong(1). If millis() - lastPlayTime >= playDuration, the code checks if the play duration has been reached and then replays the track.
else if (isPlaying && (millis() - lastPlayTime >= playDuration))
Stops the song if no smoke is detected and the set duration has passed. playSong Function
myDFPlayer.play(song)
Plays the audio file designated by song on the DFPlayer Mini.
lastPlayTime = millis()
Stores the current time to track how long the audio has been playing.
isPlaying = true
Sets the flag to indicate an audio file is playing.
Serial.println("Playing song " + String(song))
Logs the audio track playback in the Serial Monitor. stopSong Function
myDFPlayer.stop()
Stops any currently playing audio.
isPlaying = false
Resets the isPlaying flag, allowing the system to detect and play the track on the next smoke detection.
Serial.println("Track finished playing")
Logs that the audio has stopped playing.

COMPLETE CODE
#include "SoftwareSerial.h"         // Include SoftwareSerial library for serial communication
#include "DFRobotDFPlayerMini.h"    // Include DFRobotDFPlayerMini library for DFPlayer Mini

SoftwareSerial mySoftwareSerial(27, 26); // Software serial on pins 27 (RX) and 26 (TX)
DFRobotDFPlayerMini myDFPlayer;          // DFPlayerMini object for controlling the module

const int analogButtonPin = A2;          // Analog pin connected to MQ-135 sensor
bool isPlaying = false;                  // Flag to track if an audio track is playing
unsigned long lastPlayTime = 0;          // Store last play time
const unsigned long playDuration = 3500; // Play duration in milliseconds

void setup() {
    mySoftwareSerial.begin(9600);        // Start software serial at 9600 baud
    Serial.begin(115200);                // Start serial monitor at 115200 baud
    pinMode(analogButtonPin, INPUT);     // Set sensor pin as input

    if (!myDFPlayer.begin(mySoftwareSerial)) { // Initialize DFPlayer Mini
        Serial.println(F("DFPlayer Mini not initialized."));
        while (true);                    // Halt if initialization fails
    }

    Serial.println(F("DFPlayer Mini initialized!")); 
    myDFPlayer.volume(30);               // Set volume to 30 (max: 30)
    myDFPlayer.EQ(0);                    // Set EQ (0: Normal)
}

void loop() {
    int reading = analogRead(analogButtonPin); // Read analog value from sensor
    Serial.println(reading);

    // If smoke is detected (value > threshold)
    if (reading > 80) {                  
        if (!isPlaying) {                
            playSong(1);                  // Play audio file 1
        } else if (millis() - lastPlayTime >= playDuration) {
            playSong(1);                  // Restart song after play duration
        }
    } else if (isPlaying && (millis() - lastPlayTime >= playDuration)) {
        stopSong();                       // Stop song if no smoke is detected
    }
}

// Function to play audio
void playSong(uint8_t song) {
    myDFPlayer.play(song);               // Play specified audio file
    lastPlayTime = millis();             // Record play time
    isPlaying = true;                    // Set playing flag to true
    Serial.println("Playing song " + String(song));
}

// Function to stop audio
void stopSong() {
    myDFPlayer.stop();                   // Stop audio playback
    isPlaying = false;                   // Reset playing flag
    Serial.println("Track finished playing");
}
How the Project Works

In this project, we use the MQ-135 sensor to detect smoke levels. When smoke is detected (based on a threshold analog value), the DFPlayer Mini plays an audio alert from the SD card. If no smoke is present, the system stops playing the audio. This creates a basic smoke alarm with audio feedback. The SD card should contain an audio file (e.g., of a person coughing) that will play as an alert. This setup is ideal for small DIY alarm systems, helping you monitor air quality and alerting you when air pollutants exceed safe levels.
poster_article Upload Ideas T-shirt Oversize
poster_article ARDUINO CODE FOR: Water Level Measurement with Audio | ...